package com.emm.yixun.marketing.service.impl;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.jws.WebService;

import com.emm.yixun.basic.service.*;
import com.emm.yixun.common.model.NeedAdvanceRefund;
import com.emm.yixun.common.model.dto.*;
import com.emm.yixun.common.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.common.model.NeedReceiveRefund;
import com.emm.yixun.common.model.Subscribe;
import com.emm.yixun.common.model.enums.BusinessFlagEnum;
import com.emm.yixun.common.model.enums.BusinessTypeEnum;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.common.model.enums.DocTypeEnum;
import com.emm.yixun.common.model.enums.HousingFundTypeEnum;
import com.emm.yixun.common.model.enums.PaymentMethodEnum;
import com.emm.yixun.common.model.enums.TransferStatusEnum;
import com.emm.yixun.marketing.service.ProceedsServiceFacade;

@Service("proceedsServiceFacade")
@WebService(name="com.emm.yixun.marketing.service.ProceedsServiceFacade")
public class ProceedsServiceFacadeImpl implements ProceedsServiceFacade{

	@Autowired
	private SubscribeService subscribeService;
	@Autowired
	private NeedReceiveRefundService needReceiveRefundService;
	@Autowired
	private HaveReceiveRefundService haveReceiveRefundService;
	@Autowired
	private NeedAdvanceRefundService needAdvanceRefundService;
	@Autowired
	private HaveAdvanceRefundService haveAdvanceRefundService;
	@Autowired
	private ProceedsTransactionalService proceedsTransactionalService;
	@Autowired
	private SubscribeTransService subscribeTransService;
	
	
	@Override
	public Map<String, String> receiveMoney(ProceedsDto proceedsDto) throws Exception {
		Map<String, String> returnMap = new HashMap<String, String>();
		long relationId = proceedsDto.getRelationId();
		String businessType = proceedsDto.getBusinessType();
		//当前实收款
		String realAmount = proceedsDto.getRealAmount();
		BigDecimal dec_realAmount = new BigDecimal(realAmount);
		//应收款总额
		BigDecimal totalReceiveMoney = new BigDecimal(0);
		//1、获取待收款记录
		NeedReceiveRefund needReceiveRefund = new NeedReceiveRefund();
		NeedReceiveRefundDto nrrDto = new NeedReceiveRefundDto();
		nrrDto.setRelationId(relationId);
		nrrDto.setDocType(DocTypeEnum.COLLECTION.getNo());
		
		if(BusinessTypeEnum.DEPOSIT.getNo().equals(businessType)){
			//认筹记录
			nrrDto.setBusinessType(BusinessTypeEnum.DEPOSIT.getNo());
			needReceiveRefund = needReceiveRefundService.selectByDto(nrrDto);
			if(null != needReceiveRefund){
				if(null != needReceiveRefund.getTotalReceivablesAmount()){
					totalReceiveMoney = needReceiveRefund.getTotalReceivablesAmount();
				}
			}else{
				returnMap.put("returnCode", "00000");
				returnMap.put("returnMsg", "认筹收款记录不存在!");
				return returnMap;
			}
		}else if(BusinessTypeEnum.SUBSCRIBE.getNo().equals(businessType)){
			//认购记录
			nrrDto.setBusinessType(BusinessTypeEnum.SUBSCRIBE.getNo());
			needReceiveRefund = needReceiveRefundService.selectByDto(nrrDto);
			if(null != needReceiveRefund){
				if(needReceiveRefund.getTotalReceivablesAmount() != null){
					totalReceiveMoney = needReceiveRefund.getTotalReceivablesAmount();
				}
			}else{
				returnMap.put("returnCode", "00000");
				returnMap.put("returnMsg", "认购收款记录不存在!");
				return returnMap;
			}
		}else if(BusinessTypeEnum.SIGNED.getNo().equals(businessType)){
			//签约存在多条待收款记录,需根据收款记录Id获取当前待收款记录
			long needReceiveId = 0L;
			if(null == proceedsDto.getNeedReceiveId()){
				returnMap.put("returnCode", "00000");
				returnMap.put("returnMsg", "签约收款记录不存在!");
				return returnMap;
			}else{
				needReceiveId = proceedsDto.getNeedReceiveId();
			}
			needReceiveRefund = needReceiveRefundService.selectById(needReceiveId);
			if(null != needReceiveRefund){
				if(needReceiveRefund.getTotalReceivablesAmount() != null){
					totalReceiveMoney = needReceiveRefund.getTotalReceivablesAmount();
				}
			}else{
				returnMap.put("returnCode", "00000");
				returnMap.put("returnMsg", "签约收款记录不存在!");
				return returnMap;
			}
		}else if(BusinessTypeEnum.AREA.getNo().equals(businessType)){
			//面积补差收款,不在此处
			//面积补差记录
			nrrDto.setBusinessType(BusinessTypeEnum.AREA.getNo());
			needReceiveRefund = needReceiveRefundService.selectByDto(nrrDto);
			if(null != needReceiveRefund){
				if(needReceiveRefund.getTotalReceivablesAmount() != null){
					totalReceiveMoney = needReceiveRefund.getTotalReceivablesAmount();
				}
			}else{
				returnMap.put("returnCode", "00000");
				returnMap.put("returnMsg", "面积补差收款记录不存在!");
				return returnMap;
			}
		}
		if(!BusinessTypeEnum.SIGNED.getNo().equals(businessType)){
			NeedReceiveRefundDto needReceiveRefundDto = new NeedReceiveRefundDto();
			needReceiveRefundDto.setDocType(DocTypeEnum.COLLECTION.getNo());
			if(BusinessTypeEnum.DEPOSIT.getNo().equals(businessType)){
				needReceiveRefundDto.setBusinessType(BusinessTypeEnum.DEPOSIT.getNo());
			}else if(BusinessTypeEnum.SUBSCRIBE.getNo().equals(businessType)){
				needReceiveRefundDto.setBusinessType(BusinessTypeEnum.SUBSCRIBE.getNo());
			}else if(BusinessTypeEnum.AREA.getNo().equals(businessType)){
				needReceiveRefundDto.setBusinessType(BusinessTypeEnum.AREA.getNo());
			}
			needReceiveRefundDto.setRelationId(relationId);
			needReceiveRefund = needReceiveRefundService.selectByDto(needReceiveRefundDto);
		}else{
			needReceiveRefund = needReceiveRefundService.selectById(proceedsDto.getNeedReceiveId());
		}
		
		//2、比较收款金额与未收款记录
		//获取已收款总金额
		HaveReceiveRefundDto hrrfDto = new HaveReceiveRefundDto();
		hrrfDto.setRelationId(relationId);
		hrrfDto.setReceivablesType(businessType);
		hrrfDto.setDocType(needReceiveRefund.getDocType());
		//签约收款,需带上签约待收款的ID
		if(BusinessTypeEnum.SIGNED.getNo().equals(businessType)){
			hrrfDto.setReceivablesId(proceedsDto.getNeedReceiveId());
		}
		//面积补差收款,需带上面积补差待收款的ID
		if(BusinessTypeEnum.AREA.getNo().equals(businessType)){
			hrrfDto.setReceivablesId(proceedsDto.getNeedReceiveId());
		}
		Double haveReceived = haveReceiveRefundService.haveReceiveAmount(hrrfDto);
		//已收款金额转成BigDecimal
		BigDecimal dec_haveReceived = new BigDecimal("0");
		if(null != haveReceived){
			dec_haveReceived = new BigDecimal(haveReceived.toString());
		}
		//未收款总额 = 应收款总额 - 已收款总额
		BigDecimal diff_receiveMoney = totalReceiveMoney.subtract(dec_haveReceived);
		//比较未收款金额与当前实际收款金额
		if(diff_receiveMoney.compareTo(dec_realAmount) < 0){
			//当前收款金额超出剩余未收款金额
			//返回提醒
			returnMap.put("returnCode", "00000");
			returnMap.put("returnMsg", "收款金额["+dec_realAmount+"]超出剩余未收款金额["+diff_receiveMoney+"]");
			return returnMap;
		}else{
			//用于判断待收款是否已经收满
			boolean enoughFlag = (diff_receiveMoney.compareTo(dec_realAmount) == 0);
			//收款金额正常,继续进行收款操作
			returnMap = haveReceiveOperation(proceedsDto, needReceiveRefund, enoughFlag);
			if(BusinessTypeEnum.SUBSCRIBE.getNo().equals(businessType)&&enoughFlag){ //收认购金已经收满
			    SubscribeTransRequestDto requestDto = new SubscribeTransRequestDto();
			    Subscribe subscribe =  subscribeService.selectById(relationId);
			    //BigDecimal transAmount = subscribe.getSubscribeAmount().subtract(subscribe.getDepositReceivable());
			    BigDecimal transAmount = subscribe.getSubscribeAmount();
			    requestDto.setSubId(relationId);
			    requestDto.setTransAmount(transAmount);
			    subscribeTransService.subcribeTrans(requestDto);
			}
		}
		return returnMap;
	}
	
	/**
	 * 收款操作
	 * @param proceedsDto 收款相关记录
	 * @param needReceiveRefund 待收款记录
	 * @return
	 */
	public Map<String, String> haveReceiveOperation(ProceedsDto proceedsDto, NeedReceiveRefund needReceiveRefund, boolean enoughFlag){
		Map<String, String> returnMap = new HashMap<String, String>();
		//1、添加收款记录
		//1.1、判断是否存在POS转账手续费
		//1.2、存在,添加一条手续费收款
		//2、判断收款金额是否等于待收款金额
		//2.1、相等,改变待收款记录状态为已结清
		//3、存储凭证
		try{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			//添加收款记录
			HaveReceiveRefundDto haveReceiveRefundDto = new HaveReceiveRefundDto();
			haveReceiveRefundDto.setBillType(proceedsDto.getBillType());
			haveReceiveRefundDto.setPayerName(proceedsDto.getPayerName());
			haveReceiveRefundDto.setBankCode(proceedsDto.getBankCode());
			haveReceiveRefundDto.setReceivablesId(needReceiveRefund.getId());
			haveReceiveRefundDto.setRelationId(proceedsDto.getRelationId());
			haveReceiveRefundDto.setDeductibleAmount(new BigDecimal(0));
			haveReceiveRefundDto.setRealAmount(new BigDecimal(proceedsDto.getRealAmount()));
			haveReceiveRefundDto.setBillCode(proceedsDto.getBillCode());
			//结算方式可以为空 P3 需求，
			if(!com.alibaba.druid.util.StringUtils.isEmpty(proceedsDto.getStatementType())){
				haveReceiveRefundDto.setStatementType(Long.parseLong(proceedsDto.getStatementType()));
			}
			haveReceiveRefundDto.setStatementId(proceedsDto.getUserId());
			haveReceiveRefundDto.setReChecked("0");
			try {
				haveReceiveRefundDto.setStatementDate(sdf.parse(proceedsDto.getStatementDate()));
			} catch (ParseException e) {
				e.printStackTrace();
				returnMap.put("returnCode", "00000");
				returnMap.put("returnMsg", "结算日期错误");
				return returnMap;
			}
			haveReceiveRefundDto.setPaymentMethod(needReceiveRefund.getPaymentMethod());
			haveReceiveRefundDto.setReceivablesFunds(needReceiveRefund.getReceivablesFunds());
			haveReceiveRefundDto.setReceivablesType(proceedsDto.getBusinessType());
			haveReceiveRefundDto.setAgentName(proceedsDto.getAgentName());
			haveReceiveRefundDto.setOrgName(proceedsDto.getOrgName());
			haveReceiveRefundDto.setReceivablesDate(needReceiveRefund.getReceivablesDate());
			//内部转账时,使用另外一个状态.
			//目前无内部转账,此处统一使用 Normal 枚举值
			haveReceiveRefundDto.setTransferStatus(TransferStatusEnum.NORMAL.getNo());
			haveReceiveRefundDto.setCustomerId(needReceiveRefund.getCustomerId());
			haveReceiveRefundDto.setRoomId(needReceiveRefund.getRoomId());
			if(BusinessTypeEnum.DEPOSIT.getNo().equals(proceedsDto.getBusinessType())){
				//认筹,放款类型为意向金
				haveReceiveRefundDto.setHousingFundType(needReceiveRefund.getHousingFundType());
				//业务标志同意设定为收款
				haveReceiveRefundDto.setBusinessFlag(needReceiveRefund.getBusinessFlag());
			}else if(BusinessTypeEnum.SUBSCRIBE.getNo().equals(proceedsDto.getBusinessType())){
				//认购,放款类型为大定金
				haveReceiveRefundDto.setHousingFundType(needReceiveRefund.getHousingFundType());
				//业务标志同意设定为收款
				haveReceiveRefundDto.setBusinessFlag(needReceiveRefund.getBusinessFlag());
			}else if(BusinessTypeEnum.SIGNED.getNo().equals(proceedsDto.getBusinessType())){
				//签约,根据付款记录中的付款方式,决定放款类型
				if(PaymentMethodEnum.LUMP_SUM.getNo().equals(needReceiveRefund.getPaymentMethod())){
					//LUMP_SUM("1","一次性付款",null),
					haveReceiveRefundDto.setHousingFundType(needReceiveRefund.getHousingFundType());
				}else if(PaymentMethodEnum.INSTALLMENT_PAYMENT.getNo().equals(needReceiveRefund.getPaymentMethod())){
					//INSTALLMENT_PAYMENT("2","分期付款",null),
					//分期付款包括一期款、二期款、三期款等
					haveReceiveRefundDto.setHousingFundType(needReceiveRefund.getHousingFundType());
				}else if(PaymentMethodEnum.COMMERCIAL_MORTGAGE.getNo().equals(needReceiveRefund.getPaymentMethod())){
					//COMMERCIAL_MORTGAGE("3","商业按揭",null),
					//商业贷款包括首付款、商业按揭款
					if(needReceiveRefund.getReceivablesFunds().indexOf("首付") >= 0){
						haveReceiveRefundDto.setHousingFundType(needReceiveRefund.getHousingFundType());
					}else if(needReceiveRefund.getReceivablesFunds().indexOf("按揭") >= 0){
						haveReceiveRefundDto.setHousingFundType(needReceiveRefund.getHousingFundType());
					}
				}else if(PaymentMethodEnum.PROVIDENT_FUND_MORTGAGE.getNo().equals(needReceiveRefund.getPaymentMethod())){
					//PROVIDENT_FUND_MORTGAGE("4","公积金按揭",null),
					//公积金贷款包括首付款、公积金按揭款
					if(needReceiveRefund.getReceivablesFunds().indexOf("首付") >= 0){
						haveReceiveRefundDto.setHousingFundType(needReceiveRefund.getHousingFundType());
					}else if(needReceiveRefund.getReceivablesFunds().indexOf("公积金") >= 0){
						haveReceiveRefundDto.setHousingFundType(needReceiveRefund.getHousingFundType());
					}
				}else if(PaymentMethodEnum.COMBINED_LOAN.getNo().equals(needReceiveRefund.getPaymentMethod())){
					//COMBINED_LOAN("5","组合贷款",null),
					//组合贷款包括首付款、商业按揭款、公积金按揭款
					if(needReceiveRefund.getReceivablesFunds().indexOf("首付") >= 0){
						haveReceiveRefundDto.setHousingFundType(needReceiveRefund.getHousingFundType());
					}else if(needReceiveRefund.getReceivablesFunds().indexOf("按揭") >= 0){
						haveReceiveRefundDto.setHousingFundType(needReceiveRefund.getHousingFundType());
					}else if(needReceiveRefund.getReceivablesFunds().indexOf("公积金") >= 0){
						haveReceiveRefundDto.setHousingFundType(needReceiveRefund.getHousingFundType());
					}
				}else{
					haveReceiveRefundDto.setHousingFundType(needReceiveRefund.getHousingFundType());
				}
				//业务标志同意设定为收款
				haveReceiveRefundDto.setBusinessFlag(needReceiveRefund.getBusinessFlag());
				//收款类型: 收认购合同金收款统一转换为 一次性收款,业务标志为收签约金
				if(HousingFundTypeEnum.FUNDTYPE_25.getNo().equals(needReceiveRefund.getHousingFundType())){
					haveReceiveRefundDto.setHousingFundType(HousingFundTypeEnum.FUNDTYPE_5.getNo());
					haveReceiveRefundDto.setBusinessFlag(BusinessFlagEnum.BUSINESSFLAG_9.getNo());
				}
				//增加收装修款
				if(HousingFundTypeEnum.FUNDTYPE_26.getNo().equals(needReceiveRefund.getHousingFundType())){
					haveReceiveRefundDto.setHousingFundType(HousingFundTypeEnum.FUNDTYPE_26.getNo());
					haveReceiveRefundDto.setBusinessFlag(BusinessFlagEnum.BUSINESSFLAG_9.getNo());
				}
			}else if(BusinessTypeEnum.AREA.getNo().equals(proceedsDto.getBusinessType())){
				//面积补差
				haveReceiveRefundDto.setHousingFundType(needReceiveRefund.getHousingFundType());
				//业务标志同意设定为收款
				haveReceiveRefundDto.setBusinessFlag(needReceiveRefund.getBusinessFlag());
			}
			haveReceiveRefundDto.setDocType(DocTypeEnum.COLLECTION.getNo());
			haveReceiveRefundDto.setRemark(proceedsDto.getRemark());
			haveReceiveRefundDto.setMerchantId(needReceiveRefund.getMerchantId());
			haveReceiveRefundDto.setAccessCode(needReceiveRefund.getAccessCode());
			haveReceiveRefundDto.setUserCode(proceedsDto.getUserCode());
			haveReceiveRefundDto.setCreateUserId(proceedsDto.getUserId());
			haveReceiveRefundDto.setDelFlag(DelFlagEnum.UNDEL.getNo());

			haveReceiveRefundDto.setDocTypeName("收款单");
			if(!BusinessTypeEnum.DEPOSIT.getNo().equals(proceedsDto.getBusinessType())) {
				haveReceiveRefundDto.setBusinessTypeName("交易");
			}else{
				haveReceiveRefundDto.setBusinessTypeName("预约单");
			}
			if(HousingFundTypeEnum.FUNDTYPE_45.getNo().equals(needReceiveRefund.getHousingFundType())
				||HousingFundTypeEnum.FUNDTYPE_1.getNo().equals(needReceiveRefund.getHousingFundType())) {
				haveReceiveRefundDto.setGrayType("其它");
			}else if (HousingFundTypeEnum.FUNDTYPE_7.getNo().equals(needReceiveRefund.getHousingFundType())
					|| HousingFundTypeEnum.FUNDTYPE_11.getNo().equals(needReceiveRefund.getHousingFundType())
					|| HousingFundTypeEnum.FUNDTYPE_18.getNo().equals(needReceiveRefund.getHousingFundType())
					|| HousingFundTypeEnum.FUNDTYPE_19.getNo().equals(needReceiveRefund.getHousingFundType())
					|| HousingFundTypeEnum.FUNDTYPE_28.getNo().equals(needReceiveRefund.getHousingFundType())
					|| HousingFundTypeEnum.FUNDTYPE_36.getNo().equals(needReceiveRefund.getHousingFundType())
					|| HousingFundTypeEnum.FUNDTYPE_37.getNo().equals(needReceiveRefund.getHousingFundType())) {
				haveReceiveRefundDto.setGrayType("贷款类房款");
			}else {
				haveReceiveRefundDto.setGrayType("非贷款类房款");
			}
			haveReceiveRefundDto.setGratName(HousingFundTypeEnum.getByNo(needReceiveRefund.getHousingFundType()).getDes());
			//保存 收款记录,新增事物控制
			proceedsTransactionalService.haveReceiveOperationWithTransactional(proceedsDto, needReceiveRefund, enoughFlag, haveReceiveRefundDto);
			
			/*haveReceiveRefundDto = haveReceiveRefundService.create(haveReceiveRefundDto);
			
			if(StatementTypeEnum.POS_TRANSFER.getNo().equals(proceedsDto.getStatementType())){
				if(StringUtils.isNotEmpty(proceedsDto.getServiceFee()) ){
					//手续费
					haveReceiveRefundFeeDto = new HaveReceiveRefundDto();
					BeanUtils.copyProperties(haveReceiveRefundDto, haveReceiveRefundFeeDto);
					haveReceiveRefundFeeDto.setRealAmount(new BigDecimal(proceedsDto.getServiceFee()));
					haveReceiveRefundFeeDto.setStatementType(Long.parseLong(StatementTypeEnum.POS_TRANSFER.getNo()));
					//手续费皆为一次性支付
					haveReceiveRefundFeeDto.setPaymentMethod(PaymentMethodEnum.LUMP_SUM.getNo());
					//手续费无经纪人、机构
					haveReceiveRefundFeeDto.setAgentName(null);
					haveReceiveRefundFeeDto.setOrgName(null);
					//手续费营收日期定为当天
					haveReceiveRefundFeeDto.setReceivablesDate(Calendar.getInstance().getTime());
					//房款类型: POS手续费
					haveReceiveRefundFeeDto.setHousingFundType(HousingFundTypeEnum.FUNDTYPE_8.getNo());
					//业务标志: POS手续费
					haveReceiveRefundFeeDto.setBusinessFlag(BusinessFlagEnum.BUSINESSFLAG_11.getNo());
					
					haveReceiveRefundService.create(haveReceiveRefundFeeDto);
				}
			}
			
			//变更应收款记录
			//收款金额与剩余收款总额一致,变更待收款记录为已结清
			if(enoughFlag){
				NeedReceiveRefundDto needReceiveRefundDto = new NeedReceiveRefundDto();
				needReceiveRefundDto.setId(needReceiveRefund.getId());
				needReceiveRefundDto.setStatus(NeedStatusEnum.YES.getNo());
				needReceiveRefundService.update(needReceiveRefundDto);
			}
			//存储收款凭证
			long haveReceiveRefundId = haveReceiveRefundDto.getId();
			String imgType = "";
			String remark = "";
			String fileArr = proceedsDto.getFileArr();
			if(BusinessTypeEnum.DEPOSIT.getNo().equals(proceedsDto.getBusinessType())){
				imgType = ImgTypeEnum.REC_DEPOSIT.getNo();
				remark = "认筹凭证";
			}else if(BusinessTypeEnum.SUBSCRIBE.getNo().equals(proceedsDto.getBusinessType())){
				imgType = ImgTypeEnum.REC_SUBSCRIBE.getNo();
				remark = "认购凭证";
			}else if(BusinessTypeEnum.SIGNED.getNo().equals(proceedsDto.getBusinessType())){
				imgType = ImgTypeEnum.REC_SIGNED.getNo();
				remark = "签约凭证";
			}else if(BusinessTypeEnum.AREA.getNo().equals(proceedsDto.getBusinessType())){
				imgType = ImgTypeEnum.REC_AREA.getNo();
				remark = "面积补差凭证";
			}
			if(StringUtils.isNotEmpty(fileArr)){
				String fileArrList[] = fileArr.split("[|]");
				for(String file: fileArrList){
					if(StringUtils.isNotEmpty(file)){
						ImageDto imageDto = new ImageDto();
						imageDto.setObjectId(haveReceiveRefundId);
						imageDto.setTitle("收款凭证");
						imageDto.setContent("收款凭证");
						imageDto.setSmallUrl(file);
						imageDto.setImgType(imgType);
						imageDto.setMerchantId(haveReceiveRefundDto.getMerchantId());
						imageDto.setUserCode(haveReceiveRefundDto.getUserCode());
						imageDto.setAccessCode(haveReceiveRefundDto.getAccessCode());
						imageDto.setRemark(remark);
						imageDto.setCreateUserId(haveReceiveRefundDto.getCreateUserId());
						imageDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
						imageService.create(imageDto);
					}
				}
			}*/
			
			
			returnMap.put("returnCode", "00001");
			returnMap.put("returnMsg", "收款成功");
		}catch(Exception ex){
			ex.printStackTrace();
			returnMap.put("returnCode", "00000");
			returnMap.put("returnMsg", "系统异常,请联系管理员!");
			return returnMap;
		}
		return returnMap;
	}

	@Override
	public Map<String, String> receiveAdvanceMoney(ProceedsDto proceedsDto) throws Exception {
		Map<String, String> returnMap = new HashMap<String, String>();
		//当前实收款
		String realAmount = proceedsDto.getRealAmount();

		//获取待收款记录
		NeedAdvanceRefund needAdvanceRefund = needAdvanceRefundService.selectById(proceedsDto.getNeedReceiveId());
		//应收款总额
		BigDecimal totalNeedAdvance = needAdvanceRefund.getTotalReceivablesAmount();

		//比较收款金额与未收款记录
		//获取已收款总金额
		HaveAdvanceRefundDto hrrfDto = new HaveAdvanceRefundDto();
		hrrfDto.setRelationId(proceedsDto.getRelationId());
		hrrfDto.setReceivablesType(proceedsDto.getBusinessType());
		hrrfDto.setReceivablesId(proceedsDto.getNeedReceiveId());
		hrrfDto.setDelFlag("0");
		Double haveAdvanceAmount = haveAdvanceRefundService.haveAdvanceAmount(hrrfDto);

		//未收款总额 = 应收款总额 - 已收款总额
		BigDecimal surplusAmount = totalNeedAdvance.subtract(new BigDecimal(haveAdvanceAmount));
		//比较未收款金额与当前实际收款金额
		if(surplusAmount.compareTo(new BigDecimal(realAmount)) < 0){
			//当前收款金额超出剩余未收款金额
			//返回提醒
			returnMap.put("returnCode", "00000");
			returnMap.put("returnMsg", "收款金额["+realAmount+"]超出剩余未收款金额["+surplusAmount.toString()+"]");
			return returnMap;
		}else{
			//用于判断待收款是否已经收满
			boolean enoughFlag = (surplusAmount.compareTo(new BigDecimal(realAmount)) == 0);
			//收款金额正常,继续进行收款操作
			returnMap = haveAdvanceOperation(proceedsDto, needAdvanceRefund, enoughFlag);

		}
		return returnMap;
	}

	/**
	 * 收款操作
	 * @param proceedsDto 收款相关记录
	 * @param needAdvanceRefund 待收款记录
	 * @return
	 */
	public Map<String, String> haveAdvanceOperation(ProceedsDto proceedsDto, NeedAdvanceRefund needAdvanceRefund, boolean enoughFlag){
		Map<String, String> returnMap = new HashMap<String, String>();
		//1、添加收款记录
		//1.2、存在,添加一条手续费收款
		//2、判断收款金额是否等于待收款金额
		//2.1、相等,改变待收款记录状态为已结清
		//3、存储凭证
		try{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			//添加收款记录
			HaveAdvanceRefundDto haveAdvanceRefundDto = new HaveAdvanceRefundDto();
			haveAdvanceRefundDto.setBillType(proceedsDto.getBillType());
			haveAdvanceRefundDto.setPayerName(proceedsDto.getPayerName());
			haveAdvanceRefundDto.setBankCode(proceedsDto.getBankCode());
			haveAdvanceRefundDto.setReceivablesId(needAdvanceRefund.getId());
			haveAdvanceRefundDto.setRelationId(proceedsDto.getRelationId());
			haveAdvanceRefundDto.setDeductibleAmount(new BigDecimal(0));
			haveAdvanceRefundDto.setRealAmount(new BigDecimal(proceedsDto.getRealAmount()));
			haveAdvanceRefundDto.setBillCode(proceedsDto.getBillCode());
			if(!StringUtils.isEmpty(proceedsDto.getStatementType())){
				haveAdvanceRefundDto.setStatementType(Long.parseLong(proceedsDto.getStatementType()));
			}
			haveAdvanceRefundDto.setStatementId(proceedsDto.getUserId());
			haveAdvanceRefundDto.setReChecked("0");
			haveAdvanceRefundDto.setStatementDate(DateUtils.StringToDate(proceedsDto.getStatementDate(), DateUtils.parsePatterns[1]));
			haveAdvanceRefundDto.setPaymentMethod(needAdvanceRefund.getPaymentMethod());
			haveAdvanceRefundDto.setReceivablesFunds(needAdvanceRefund.getReceivablesFunds());
			haveAdvanceRefundDto.setReceivablesType(proceedsDto.getBusinessType());
			haveAdvanceRefundDto.setAgentName(proceedsDto.getAgentName());
			haveAdvanceRefundDto.setOrgName(proceedsDto.getOrgName());
			haveAdvanceRefundDto.setReceivablesDate(needAdvanceRefund.getReceivablesDate());
			haveAdvanceRefundDto.setTransferStatus(TransferStatusEnum.NORMAL.getNo());
			haveAdvanceRefundDto.setCustomerId(needAdvanceRefund.getCustomerId());
			haveAdvanceRefundDto.setRoomId(needAdvanceRefund.getRoomId());
			haveAdvanceRefundDto.setHousingFundType(HousingFundTypeEnum.FUNDTYPE_45.getNo());
			haveAdvanceRefundDto.setBusinessFlag(BusinessFlagEnum.BUSINESSFLAG_9.getNo());
			haveAdvanceRefundDto.setDocType(DocTypeEnum.COLLECTION.getNo());
			haveAdvanceRefundDto.setRemark(proceedsDto.getRemark());
			haveAdvanceRefundDto.setMerchantId(needAdvanceRefund.getMerchantId());
			haveAdvanceRefundDto.setAccessCode(needAdvanceRefund.getAccessCode());
			haveAdvanceRefundDto.setUserCode(proceedsDto.getUserCode());
			haveAdvanceRefundDto.setCreateUserId(proceedsDto.getUserId());
			haveAdvanceRefundDto.setBusinessTypeName("交易");
			haveAdvanceRefundDto.setDocTypeName("收款单");
			haveAdvanceRefundDto.setGrayType("其它");
			haveAdvanceRefundDto.setGratName("垫付首期");
			haveAdvanceRefundDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
			//保存 收款记录,新增事物控制
			proceedsTransactionalService.haveAdvanceOperationWithTransactional(proceedsDto, needAdvanceRefund, haveAdvanceRefundDto, enoughFlag);

			returnMap.put("returnCode", "00001");
			returnMap.put("returnMsg", "收款成功");
		}catch(Exception ex){
			ex.printStackTrace();
			returnMap.put("returnCode", "00000");
			returnMap.put("returnMsg", "系统异常,请联系管理员!");
			return returnMap;
		}
		return returnMap;
	}
	
}
